<?php
// $Id: qpager.class.php 13 2011-04-26 02:41:59Z tomqin@gmail.com $

/**
 * QPager 分頁管理
 *
 * @author TomQin <tomqin@gmail.com>
 * @copyright Copyright (c) 2009-2012 Refang.Net Inc.
 * @package Core
 */

class QPager{

	/* db 数据源 */
	public $db = null; 			    //数据库连接句柄
	public $db_query   = '';		//查询语句

	/* 分页显示参数设置 */
	public $page_size  = 0;  		//每页显示记录数目
	public $link_num   = 5;   		//每页页码链接数目
	public $page       = 1;  		//页码
	public $records    = -1; 		//表中记录总数
	public $page_count = 0;  	    //总页数
	public $pagestring = '';    	//前后分页链接字符串

	public function __construct(&$db){
		$this->db = $db;
	}

	/**
	 * 设置总记录数
	 *
	 * @param integer 总记录数
	 * @return void
	 */
	public function totalRecords($records){
		$this->records = intval($records);
	}


	/**
	 * 设置当前页
	 *
	 * @param integer 当前页码
	 * @return void
	 */
	public function curPage($page = 0){
		if(empty($page)){
			if(isset($_REQUEST['page']) && $_REQUEST['page']!=null){
				$page = $_REQUEST['page'];
			}else{
				$page = 1;
			}
		}

		$this->page = intval($page);
	}

	/**
	 * 初始化相关参数
	 *
	 * @param string   等待查询 SQL 语句
	 * @param integer  每页显示记录数目
	 * @param integer  每页页码链接数目
	 * @return	object
	 */
	public function prepare($db_query, $page_size, $link_num = 5){
		$this->db_query = $db_query;
		$this->page_size = $page_size;
		$this->link_num  = $link_num;

		$this->getRecords();
		$this->resetPage();
	}

	/**
	 * 獲取db中的記錄
	 *
	 * @return array
	 */
	public function fetchData(){
		if($this->records){
			return $this->db->getAll($this->db_query.' limit '.($this->page - 1)*$this->page_size.', '.$this->page_size);
		}else{
			return array();
		}
	}

	public function pageLink($link = '?page=%d', $class = 'pagination'){
		//頁碼鏈接字符串
		if($this->page <= ceil($this->link_num / 2)) {
			$front_page = 1;
			$last_page  = ($this->page_count < $this->link_num) ? $this->page_count : $this->link_num;
			$t = 1;
		}elseif($this->page + ceil($this->link_num / 2)  > $this->page_count){
			$front_page = ($this->page_count < $this->link_num) ? 1 : $this->page_count - $this->link_num + 1;
			$last_page  = $this->page_count;
			$t = 2;
		} else {
			$front_page = $this->page - ceil($this->link_num / 2) + 1;
			$last_page  = $this->page + floor($this->link_num / 2);
			$t = 3;
		}
		//echo "<h2>$t : $front_page ~ $last_page</h2>";

		$this->pagestring = '';
		$this->pagestring .= '<div class="'.$class.'">';

		$this->pagestring .= '&lt;&lt; <a href="'.$this->link($link, 1).'">首</a> &lt; <a href="'.$this->link($link, $front_page).'">上一頁</a> ';
		$comma = ' ';
		for($i=$front_page; $i <= $last_page; $i++){
			$this->pagestring .= $comma;
			if($i == $this->page) $this->pagestring .= /*'<b class="current">'.$i.'</b> ';*/ '<a class="current" href="javascript:void(0);">'.$i.'</a>';
			else $this->pagestring .= '<a href="'.$this->link($link, $i).'">'.$i.'</a> ';
			$comma = ' | ';
		}
		$this->pagestring .= ' <a href="'.$this->link($link, $last_page).'">下一頁</a> &gt; <a href="'.$this->link($link, $this->page_count).'">末</a> &gt;&gt;';
		$this->pagestring .= '</div>';

		return $this->pagestring;
	}

	/**
	 * 重新设置页码
	 *
	 * @return void
	 */
	private function resetPage(){
		$this->page_count= ceil($this->records/$this->page_size);

		if($this->page){
			$this->page = ($this->page > $this->page_count) ? $this->page_count : (($this->page <= 0) ? 1 : $this->page);
		}else{
			$this->page = 1;
		}
	}

	/**
	 * 獲取db中記錄的數目
	 *
	 * @return int
	 */
	private function getRecords(){
		if($this->records < 0 || $this->records === null){
			$sql = preg_replace("/^SELECT(.*)FROM(.*)$/iUm", "select count(1) as num from \\2", $this->db_query);
			$this->records = $this->db->getFirst($sql);
		}
	}

	/**
	 * 拼接分頁鏈接
	 *
	 * @return int
	 */
	private function link($link, $page) {
		return sprintf($link, $page);
	}
}
?>